package com.ambitor.ipro.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class DateUtil {
	
	public final static Date LATEST_TIME = str2date("8999-12-21", "yyyy-MM-dd");
	public final static Date EARLIEST_TIME = str2date("2011-01-01", "yyyy-MM-dd");
	
	public static Date str2date(String str, String fmt) {
		DateFormat df = new SimpleDateFormat(fmt);
		try {
			return df.parse(str);
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static String getToday() {
		Date today = new Date();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(today);
	}

	public static boolean inPeriod(Date start, Date end) {
		Calendar now = Calendar.getInstance();
		long tm = now.getTimeInMillis();
		if(tm>=start.getTime() && tm<=end.getTime()) return true;
		else return false;
	}
	
	public static int daysBetween(Date date1, Date date2) {
		if (date1 == null || date2 == null) {
			return 0;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date1);
		long time1 = cal.getTimeInMillis();
		cal.setTime(date2);
		long time2 = cal.getTimeInMillis();
		Long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return between_days.intValue();
	}

	public static String formatDate(Date date) {
		if (date == null)
			return StringUtils.EMPTY;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	public static String formatDate(long date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date(date));
	}

	public static String formatDateOfDayStart(Date date) {
		if (date == null)
			return StringUtils.EMPTY;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:01");
		return sdf.format(date);
	}

	public static Date getDateOfDayStart() {
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd 00:00:01");
		Calendar c = Calendar.getInstance();
		c.setTime(Timestamp.valueOf(fmt.format(System.currentTimeMillis())));
		return c.getTime();
	}

	public static String formatDateOfDayStart(long date) {
		return formatDateOfDayStart(new Date(date));
	}

	public static String format2YMD(Date date) {
		if (date == null)
			return StringUtils.EMPTY;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}
	
	public static String format(Date date, String fmt) {
		if (date == null)
			return StringUtils.EMPTY;
		SimpleDateFormat sdf = new SimpleDateFormat(fmt);
		return sdf.format(date);
	}
	
	public static void main(String [] args) {
		Map<String, String> tm = new HashMap<String, String>();
		tm.put("test", "1");
		System.out.println(tm);
		for(Map.Entry<String, String> entry : tm.entrySet()) {
			entry.setValue("test");
		}
		System.out.println(tm);
	}
}
